<template>
  <div class="login-container" :class="[themeMode, { 'cyberpunk-active': isCyberPunk }]">
    <!-- 主题切换按钮 -->
    <div class="theme-toggle">
      <button @click="toggleTheme" class="theme-btn" :aria-label="themeMode === 'light' ? '切换到暗色主题' : '切换到亮色主题'">
        <span v-if="themeMode === 'light'">🌙</span>
        <span v-else>☀️</span>
      </button>
      <button @click="toggleCyberPunk" class="cyberpunk-btn" :aria-label="isCyberPunk ? '关闭赛博朋克模式' : '开启赛博朋克模式'">
        {{ isCyberPunk ? '关闭赛博朋克' : '开启赛博朋克' }}
      </button>
    </div>

    <div class="login-box" :class="{ 'cyberpunk': isCyberPunk }">
      <div class="login-header">
        <h2 v-if="!isCyberPunk">欢迎登录</h2>
        <h2 v-else class="cyber-title">CYBER LOGIN</h2>
        <p v-if="!isCyberPunk">请输入您的账号和密码</p>
        <p v-else class="cyber-subtitle">ACCESS REQUIRED</p>
      </div>

      <form @submit.prevent="login" class="login-form">
        <div class="form-group">
          <div class="input-wrapper" :class="{ 'cyber-input': isCyberPunk, 'error': usernameError }">
            <i class="icon-user"></i>
            <input
                type="text"
                v-model="user.username"
                placeholder=" "
                class="login-input"
                @focus="clearError('username')"
                required
            >
            <label class="input-label">用户名</label>
          </div>
          <div v-if="usernameError" class="error-message">{{ usernameError }}</div>
        </div>

        <div class="form-group">
          <div class="input-wrapper" :class="{ 'cyber-input': isCyberPunk, 'error': passwordError }">
            <i class="icon-lock"></i>
            <input
                type="password"
                v-model="user.password"
                placeholder=" "
                class="login-input"
                @focus="clearError('password')"
                required
            >
            <label class="input-label">密码</label>
          </div>
          <div v-if="passwordError" class="error-message">{{ passwordError }}</div>
        </div>

        <div class="form-group">
          <button type="submit" class="login-button" :class="{ 'cyber-button': isCyberPunk }" :disabled="loading">
            <div class="button-content">
              <div v-if="loading" class="spinner"></div>
              <span>{{ loading ? (isCyberPunk ? 'VERIFYING...' : '登录中...') : (isCyberPunk ? 'ACCESS' : '登录') }}</span>
            </div>
          </button>
        </div>
      </form>

      <div class="login-footer">
        <p v-if="!isCyberPunk">还没有账号？<a href="#" @click.prevent="register">立即注册</a></p>
        <p v-else class="cyber-footer">NO ACCESS? <a href="#" @click.prevent="register">REQUEST ACCESS</a></p>
      </div>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/user'

export default {
  name: "loginview",
  data() {
    return {
      user: {
        username: '',
        password: ''
      },
      loading: false,
      themeMode: 'dark', // 默认暗色模式
      isCyberPunk: false, // 赛博朋克模式开关
      usernameError: '',
      passwordError: ''
    }
  },
  methods: {
    async login() {
      // 验证输入
      if (!this.validateForm()) return;

      this.loading = true;
      try {
        const res = await login(this.user);
        if (res.data.code === 200) {
          this.$store.commit('setUsername', this.user.username);
          this.$store.commit('setToken', res.data.data);
          this.$message.success('登录成功');
          this.$router.push('/userindex');
        } else {
          this.$message.error(res.data.msg || '登录失败');
        }
      } catch (err) {
        this.$message.error('登录请求失败，请稍后重试');
        console.error(err);
      } finally {
        this.loading = false;
      }
    }
    ,register() {
      this.$router.push('/register');
    },
    toggleTheme() {
      this.themeMode = this.themeMode === 'light' ? 'dark' : 'light';
    },
    toggleCyberPunk() {
      this.isCyberPunk = !this.isCyberPunk;
    },
    validateForm() {
      this.usernameError = '';
      this.passwordError = '';

      let isValid = true;

      if (!this.user.username.trim()) {
        this.usernameError = '请输入用户名';
        isValid = false;
      }

      if (!this.user.password) {
        this.passwordError = '请输入密码';
        isValid = false;
      }

      return isValid;
    },
    clearError(field) {
      if (field === 'username') {
        this.usernameError = '';
      } else if (field === 'password') {
        this.passwordError = '';
      }
    }
  }
}
</script>

<style scoped>
/* 基础暗色主题 */
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  padding: 20px;
  transition: all 0.5s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
}

/* 暗色模式 */
.login-container.dark {
  background: linear-gradient(135deg, #0f0c29, #302b63, #24243e);
  color: #00ffff;
}

/* 亮色模式 */
.login-container.light {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #333;
}

/* 赛博朋克激活状态 - 增强背景效果 */
.login-container.cyberpunk-active {
  background:
      linear-gradient(rgba(0, 0, 0, 0.85), rgba(0, 0, 0, 0.85)),
      repeating-linear-gradient(
          0deg,
          transparent,
          transparent 2px,
          rgba(0, 255, 255, 0.15) 2px,
          rgba(0, 255, 255, 0.15) 4px
      ),
      repeating-linear-gradient(
          90deg,
          transparent,
          transparent 2px,
          rgba(0, 255, 255, 0.05) 2px,
          rgba(0, 255, 255, 0.05) 4px
      );
  animation: cyberGrid 4s linear infinite;
  position: relative;
  overflow: hidden;
}

.login-container.cyberpunk-active::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background:
      radial-gradient(circle at 20% 30%, rgba(0, 255, 255, 0.1) 0%, transparent 20%),
      radial-gradient(circle at 80% 70%, rgba(0, 136, 255, 0.1) 0%, transparent 20%);
  pointer-events: none;
  z-index: 0;
}

@keyframes cyberGrid {
  0% {
    background-position: 0 0, 0 0;
  }
  100% {
    background-position: 0 4px, 0 0;
  }
}

.theme-toggle {
  position: absolute;
  top: 20px;
  right: 20px;
  display: flex;
  gap: 10px;
  z-index: 10;
}

.theme-btn, .cyberpunk-btn {
  padding: 8px 12px;
  border: none;
  border-radius: 20px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s ease;
  background: rgba(255, 255, 255, 0.1);
  color: inherit;
  backdrop-filter: blur(10px);
  position: relative;
  overflow: hidden;
}

.theme-btn:hover, .cyberpunk-btn:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.theme-btn::after, .cyberpunk-btn::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 0;
  height: 0;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 50%;
  transform: translate(-50%, -50%);
  transition: width 0.6s, height 0.6s;
}

.theme-btn:active::after, .cyberpunk-btn:active::after {
  width: 300px;
  height: 300px;
}

.login-box {
  width: 100%;
  max-width: 400px;
  border-radius: 20px;
  padding: 40px 30px;
  backdrop-filter: blur(10px);
  animation: slideUp 0.6s ease-out;
  transition: all 0.5s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
  z-index: 2;
}

/* 暗色模式登录框 */
.login-container.dark .login-box {
  background: rgba(30, 30, 46, 0.8);
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.5);
  border: 1px solid rgba(100, 100, 255, 0.2);
}

/* 亮色模式登录框 */
.login-container.light .login-box {
  background: rgba(230, 230, 230, 0.95);
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
}

/* 赛博朋克模式 */
.login-box.cyberpunk {
  background: rgba(0, 0, 0, 0.9);
  border: 2px solid #00ffff;
  box-shadow:
      0 0 10px #00ffff,
      0 0 20px rgba(0, 255, 255, 0.5),
      inset 0 0 10px rgba(0, 255, 255, 0.3);
  animation: cyberGlow 2s ease-in-out infinite alternate;
}

.login-box.cyberpunk::before {
  content: '';
  position: absolute;
  top: -5px;
  left: -5px;
  right: -5px;
  bottom: -5px;
  background: linear-gradient(45deg, #00ffff, #0088ff);
  z-index: -2;
  border-radius: 25px;
  opacity: 0.5;
  filter: blur(10px);
}

.login-box.cyberpunk::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background:
      linear-gradient(90deg, transparent 50%, rgba(0, 255, 255, 0.1) 50%),
      linear-gradient(transparent 50%, rgba(0, 255, 255, 0.1) 50%);
  background-size: 20px 20px;
  opacity: 0.3;
  pointer-events: none;
  animation: dataFlow 2s linear infinite;
  z-index: -1;
}

@keyframes dataFlow {
  0% { background-position: 0 0; }
  100% { background-position: 20px 20px; }
}

@keyframes cyberGlow {
  from {
    box-shadow: 0 0 10px #00ffff, 0 0 20px rgba(0, 255, 255, 0.5), inset 0 0 10px rgba(0, 255, 255, 0.3);
  }
  to {
    box-shadow: 0 0 15px #00ffff, 0 0 30px rgba(0, 255, 255, 0.7), inset 0 0 15px rgba(0, 255, 255, 0.5);
  }
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  font-size: 28px;
  margin-bottom: 10px;
  font-weight: 600;
  transition: all 0.3s ease;
}

.cyber-title {
  font-family: 'Courier New', monospace;
  letter-spacing: 3px;
  text-shadow: 0 0 10px #00ffff;
  animation: textGlow 2s ease-in-out infinite alternate;
}

@keyframes textGlow {
  from { text-shadow: 0 0 5px #00ffff; }
  to { text-shadow: 0 0 15px #00ffff, 0 0 20px #00ffff; }
}

.login-header p {
  font-size: 14px;
  transition: all 0.3s ease;
}

.cyber-subtitle {
  font-family: 'Courier New', monospace;
  letter-spacing: 1px;
  color: #00ffff;
}

.form-group {
  margin-bottom: 25px;
  position: relative;
}

.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
  border-radius: 12px;
  transition: all 0.3s ease;
  overflow: hidden;
}

/* 暗色模式输入框 */
.login-container.dark .input-wrapper {
  border: 2px solid rgba(100, 100, 255, 0.3);
  background: rgba(15, 15, 25, 0.7); /* 更深的背景色 */
}

.login-container.dark .input-wrapper:focus-within {
  border-color: #00ffff;
  box-shadow: 0 0 0 3px rgba(0, 255, 255, 0.2);
}

/* 亮色模式输入框 */
.login-container.light .input-wrapper {
  border: 2px solid #e1e5e9;
  background: #fff;
}

.login-container.light .input-wrapper:focus-within {
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

/* 赛博朋克输入框 */
.input-wrapper.cyber-input {
  border: 2px solid #00ffff;
  background: rgba(0, 0, 0, 0.8);
  box-shadow: inset 0 0 10px rgba(0, 255, 255, 0.2);
}

.input-wrapper.cyber-input:focus-within {
  box-shadow:
      0 0 10px #00ffff,
      inset 0 0 10px rgba(0, 255, 255, 0.5);
}

.input-wrapper.cyber-input::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(90deg, transparent, rgba(0, 255, 255, 0.3), transparent);
  opacity: 0;
  pointer-events: none;
  border-radius: 12px;
}

.input-wrapper.cyber-input:hover::before {
  animation: scanLine 3s linear infinite;
}

@keyframes scanLine {
  0% { transform: translateX(-100%); opacity: 0; }
  50% { opacity: 1; }
  100% { transform: translateX(100%); opacity: 0; }
}

.input-wrapper .icon-user,
.input-wrapper .icon-lock {
  width: 20px;
  height: 20px;
  margin-left: 15px;
  margin-right: 10px; /* 增加右边距 */
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
  transition: all 0.3s ease;
  z-index: 1;
}

.input-wrapper .icon-user {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%23999'%3E%3Cpath d='M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z'/%3E%3C/svg%3E");
}

.input-wrapper .icon-lock {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%23999'%3E%3Cpath d='M18 8h-1V6c0-2.76-2.24-5-5-5S7 3.24 7 6v2H6c-1.1 0-2 .9-2 2v10c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2V10c0-1.1-.9-2-2-2zm-6 9c-1.1 0-2-.9-2-2s.9-2 2-2 2 .9 2 2-.9 2-2 2zm3.1-9H8.9V6c0-1.71 1.39-3.1 3.1-3.1 1.71 0 3.1 1.39 3.1 3.1v2z'/%3E%3C/svg%3E");
}

/* 赛博朋克图标 */
.input-wrapper.cyber-input .icon-user,
.input-wrapper.cyber-input .icon-lock {
  filter: drop-shadow(0 0 5px #00ffff);
}

.login-input {
  flex: 1;
  border: none;
  padding: 15px 15px 15px 5px; /* 调整左边内边距 */
  font-size: 16px;
  outline: none;
  background: transparent;
  border-radius: 0 12px 12px 0;
  transition: all 0.3s ease;
  z-index: 1;
  color: #ffffff; /* 提高文字可读性 */
}

.login-input::placeholder {
  color: transparent;
}

/* 暗色模式文字颜色优化 */
.login-container.dark .login-input {
  color: #ffffff;
}

.login-container.dark .input-label {
  color: #cccccc;
}

/* 赛博朋克输入框文字 */
.input-wrapper.cyber-input .login-input {
  color: #00ffff;
  text-shadow: 0 0 5px rgba(0, 255, 255, 0.5);
}

.input-wrapper.cyber-input .login-input::placeholder {
  color: rgba(0, 255, 255, 0.5);
}

/* 浮动标签样式 */
.input-label {
  position: absolute;
  top: 50%;
  left: 45px;
  transform: translateY(-50%);
  color: #999;
  pointer-events: none;
  transition: all 0.3s ease;
  background: inherit;
  padding: 0 5px;
  z-index: 1;
  font-size: 16px;
}

.login-input:focus + .input-label,
.login-input:not(:placeholder-shown) + .input-label {
  top: -10px;
  left: 10px;
  font-size: 12px;
  color: #667eea;
  background: inherit;
  padding: 0 5px;
}

.input-wrapper.cyber-input .login-input:focus + .input-label,
.input-wrapper.cyber-input .login-input:not(:placeholder-shown) + .input-label {
  color: #00ffff;
  text-shadow: 0 0 5px rgba(0, 255, 255, 0.5);
}

/* 错误状态 */
.input-wrapper.error {
  border-color: #ff4757;
  animation: shake 0.5s ease-in-out;
}

.input-wrapper.error.cyber-input {
  border-color: #ff0055;
  box-shadow: 0 0 10px #ff0055;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  75% { transform: translateX(5px); }
}

.error-message {
  color: #ff4757;
  font-size: 12px;
  margin-top: 5px;
  margin-left: 10px;
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(-5px); }
  to { opacity: 1; transform: translateY(0); }
}

.login-button {
  width: 100%;
  padding: 15px;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 暗色模式按钮 */
.login-container.dark .login-button {
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: white;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}

.login-container.dark .login-button:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

/* 亮色模式按钮 */
.login-container.light .login-button {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}

.login-container.light .login-button:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

/* 赛博朋克按钮 */
.login-button.cyber-button {
  background: linear-gradient(135deg, #00ffff, #0088ff);
  color: #000;
  font-weight: bold;
  letter-spacing: 1px;
  box-shadow:
      0 0 10px #00ffff,
      0 0 20px rgba(0, 255, 255, 0.5);
  border: 1px solid #00ffff;
  text-shadow: 0 0 5px rgba(0, 0, 0, 0.5);
  animation: buttonGlow 1.5s ease-in-out infinite alternate;
}

.login-button.cyber-button:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow:
      0 0 15px #00ffff,
      0 0 30px rgba(0, 255, 255, 0.7);
}

.login-button.cyber-button:active {
  transform: translateY(-1px);
  box-shadow:
      0 0 20px #00ffff,
      0 0 40px rgba(0, 255, 255, 0.8);
}

.login-button::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 0;
  height: 0;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  transform: translate(-50%, -50%);
  transition: width 0.6s, height 0.6s;
}

.login-button:active::after {
  width: 300px;
  height: 300px;
}

.login-button:disabled {
  opacity: 0.7;
  cursor: not-allowed;
  transform: none !important;
}

/* 加载动画样式 */
.spinner {
  width: 16px;
  height: 16px;
  border: 2px solid transparent;
  border-top: 2px solid currentColor;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-right: 8px;
  display: inline-block;
  vertical-align: middle;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.button-content {
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-footer {
  text-align: center;
  margin-top: 20px;
}

.login-footer p {
  font-size: 14px;
}

/* 暗色模式文字 */
.login-container.dark .login-header h2,
.login-container.dark .login-header p,
.login-container.dark .login-footer p {
  color: #e0e0ff;
}

/* 亮色模式文字 */
.login-container.light .login-header h2,
.login-container.light .login-header p,
.login-container.light .login-footer p {
  color: #333;
}

.login-footer a {
  text-decoration: none;
  font-weight: 600;
  transition: all 0.3s ease;
  position: relative;
}

/* 暗色模式链接 */
.login-container.dark .login-footer a {
  color: #667eea;
}

.login-container.dark .login-footer a:hover {
  color: #764ba2;
  text-decoration: underline;
}

/* 亮色模式链接 */
.login-container.light .login-footer a {
  color: #667eea;
}

.login-container.light .login-footer a:hover {
  color: #764ba2;
  text-decoration: underline;
}

/* 赛博朋克链接 */
.login-box.cyberpunk .login-footer a {
  color: #00ffff;
  text-shadow: 0 0 5px rgba(0, 255, 255, 0.5);
}

.login-box.cyberpunk .login-footer a:hover {
  text-shadow: 0 0 10px #00ffff;
  text-decoration: underline;
}

.login-box.cyberpunk .login-footer a::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 0;
  height: 1px;
  background: #00ffff;
  transition: width 0.3s ease;
}

.login-box.cyberpunk .login-footer a:hover::after {
  width: 100%;
}

.cyber-footer {
  font-family: 'Courier New', monospace;
  letter-spacing: 1px;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-box {
    padding: 30px 20px;
  }

  .login-header h2 {
    font-size: 24px;
  }

  .theme-toggle {
    top: 10px;
    right: 10px;
  }

  .input-label {
    font-size: 14px;
  }
}

/* 平板设备优化 */
@media (min-width: 481px) and (max-width: 768px) {
  .login-box {
    padding: 35px 25px;
    max-width: 350px;
  }

  .login-header h2 {
    font-size: 26px;
  }

  .login-input {
    padding: 14px 12px 14px 8px;
  }
}

/* 大屏设备优化 */
@media (min-width: 1200px) {
  .login-box {
    max-width: 450px;
    padding: 50px 40px;
  }

  .login-header h2 {
    font-size: 32px;
  }

  .login-input {
    padding: 16px 20px 16px 15px;
    font-size: 17px;
  }
}

/* 键盘焦点样式 */
.login-input:focus-visible {
  outline: 2px solid #667eea;
  outline-offset: 2px;
}

.input-wrapper.cyber-input .login-input:focus-visible {
  outline: 2px solid #00ffff;
  outline-offset: 2px;
}
</style>

